/*
 * Copyright (C) 2020/12/12
 * by fred
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fred.colorpicker.indicator

import android.content.Context
import android.graphics.*
import android.graphics.Paint.ANTI_ALIAS_FLAG
import android.util.Log
import com.fred.colorpicker.dip
import kotlin.math.pow

class RingColorWheelIndicator(context: Context): ColorWheelIndicator(context) {

    private var radius = context.dip(14F)
    private val paint by lazy {
        Paint(ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.STROKE
            strokeWidth = context.dip(3F)
            color = Color.BLACK
        }
    }

    private val paint1 by lazy {
        Paint(ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.STROKE
            strokeWidth = context.dip(2F)
            color = Color.WHITE
        }
    }
    private val ringRect by lazy {
        RectF()
    }
    private val pointPaint by lazy {
        Paint(ANTI_ALIAS_FLAG).apply {
            strokeWidth = context.dip(1F)
            color = Color.BLACK
        }
    }

    init {
        defaultHRatio = 0.55F
        defaultVRatio = 0.85F
        defaultGrayRatio = 0.7F
        defaultColor = -16750757
        defaultSaturation = 0.8F
        defaultBrightness = 0.2F
    }

    override fun copy(): ColorWheelIndicator {
        return RingColorWheelIndicator(context)
    }

    override fun isInside(x: Float, y: Float): Boolean {
        val dx = currentPoint.x - x
        val dy = currentPoint.y - y
        return dx.pow(2) + dy.pow(2) <= (radius+paddingSize).pow(2)
    }

    override fun drawSelector(canvas: Canvas, point: PointF) {
        if (isSelected) {
            if (isDrawCentre) {
                val s = radius / 3
                canvas.drawLine(point.x - s, point.y, point.x + s, point.y, pointPaint)
                canvas.drawLine(point.x, point.y - s, point.x, point.y + s, pointPaint)
            }
            paint.alpha = 255
            paint1.alpha = 255
        }else{
            paint.alpha = 125
            paint1.alpha = 125
        }
        val size = radius
        ringRect.set(point.x - size,point.y - size,point.x + size,point.y+ size)
        canvas.drawArc(ringRect, 360F, 360F, false, paint)
        canvas.drawArc(ringRect, 360F, 360F, false, paint1)
    }

    override fun minimumWidth(): Int = (2*radius).toInt()

    override fun minimumHeight(): Int = (2*radius).toInt()


}